package com.video;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
import com.camarasmactel.MenuPrincipal;
import com.camarasmactel.R;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings.ZoomDensity;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;

public class Video extends Activity {

	WebView webView;
	Button grabar, volver;
	TextView fechahora;
	String ruta="";
	ProgressDialog pd;
	JavaScriptInterface JSInterface;
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
			
			webView.getSettings().setBuiltInZoomControls(true);
			webView.getSettings().setSupportZoom(true);
			webView.getSettings().setDefaultZoom(ZoomDensity.FAR);
			
		return super.onTouchEvent(event);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
	
		pd = ProgressDialog.show(this, "Conectando...", "Conectando...");	
		super.onCreate(savedInstanceState);
		setContentView(R.layout.video);
		
		ruta = MenuPrincipal.dns+":"+MenuPrincipal.puerto; 
		fechahora = (TextView) findViewById(R.id.fechahora);
		grabar = (Button) findViewById(R.id.guardarv);
		grabar.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
			}
		});
		
		volver = (Button) findViewById(R.id.volverv);
		volver.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Video.this.finish();
			}
		});
		
		webView = (WebView) findViewById(R.id.video);
		webView.getSettings().setJavaScriptEnabled(true);
		webView.getSettings().setBuiltInZoomControls(true);
		webView.getSettings().setSupportZoom(true);
		webView.getSettings().setDefaultZoom(ZoomDensity.CLOSE);
		JSInterface = new JavaScriptInterface(this);
	    webView.addJavascriptInterface(JSInterface, "JSInterface"); 
		
		//	webView.setAnimationCacheEnabled(true);
	//	webView.setDrawingCacheEnabled(true);
		String url = "file:///android_asset/android-page.html"; //?cad=http://"+ruta+"/tmpfs/auto.jpg?";
		webView.loadUrl(url);
		
		Timer time = new Timer();	
		final Handler mhandler = new Handler();
		
		final Runnable actualizar = new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				
				Calendar c = Calendar.getInstance();
				String hora = c.getTime().toString();
				SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
		        String formato = df.format(c.getTime());
				String total =formato+"  "+hora; 
				fechahora.setText(total);
			}
		};
		
		time.scheduleAtFixedRate(new TimerTask() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				
				mhandler.post(actualizar);
			}	
								
		}, 0, 1000l);
		
		pd.dismiss();
	}

	public class JavaScriptInterface {
		
		Context mContext;

        /** Instantiate the interface and set the context */
        JavaScriptInterface(Context c) {
            mContext = c;
        }

        public String ruta()
        {	
        	Log.i("ruta", "http://"+ruta+"/tmpfs/auto.jpg?");
        	return "http://"+ruta+"/tmpfs/auto.jpg?";
        }
    }

	
}
